package gui;

import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class GuiStatusBar extends JPanel {

	private JLabel statusLabel, connectionLabel;
	private JProgressBar progressBar;
	private Color textColor;
	private int filesExpected, fileProgress;
	private long bytesExpected, byteProgress;
	private boolean fileTransferInProgress;

	public GuiStatusBar() {

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setBorder(new TitledBorder("Status"));

		connectionLabel = new JLabel(" ");
		statusLabel = new JLabel("Sync IT");
		textColor = connectionLabel.getForeground();

		progressBar = new JProgressBar();
		progressBar.setValue(0);
		progressBar.setSize(20, 20);
		progressBar.setStringPainted(true);
		progressBar.setVisible(false);

		add(connectionLabel);
		add(statusLabel);
		add(progressBar);

		fileTransferInProgress = false;
	}


	// Methods that comes from LogToGuiObserver, sent through the mainGui //

	public void connectionStatusChanged(String message) {
		connectionLabel.setForeground(textColor);
		connectionLabel.setText(message);
		connectionLabel.repaint();
	}

	public void connectionStatusWarning(String message) {
		connectionLabel.setForeground(Color.red);
		connectionLabel.setText(message.split(";")[0]); // Do not print out the time
		connectionLabel.repaint();

		// Removes the progressBar if anything goes wrong
		progressBar.setVisible(false);
	}

	public void transferMessage(String file) {
		// Simple filter to print out the right thing
		if(file.startsWith("Folders are synced") || file.startsWith("Sync session done")) {
			String tempStr = file.split(";")[0] + ": " + file.split(";")[1];
			// Prints out the string ':' the time on the statusBar
			statusLabel.setText(tempStr);

			// Removes the progressBar
			progressBar.setVisible(false);
		}
		else {
			// Prints out only the string on the statusBar
			statusLabel.setText(file.split(";")[0]);
		}
		statusLabel.repaint();

		// Starts the progressBar in indeterminate mode
		if(file.startsWith("Gathering data")) {
			progressBar.setValue(0);
			progressBar.setString(" ");
			progressBar.setIndeterminate(true);
			progressBar.setVisible(true);
		}
	}


	// Methods that comes from GuiChangeObserver, sent through the mainGui //

	public void initFileTransfer(int noOfFiles, long totalOfBytes) {
		filesExpected = noOfFiles;
		fileProgress = 0;
		bytesExpected = totalOfBytes;
		byteProgress = 0;
		progressBar.setMinimum(0);
		progressBar.setMaximum((int)(bytesExpected/10)); // WARNING This solution gives a correct progressBar for data up to 20 GB
		progressBar.setValue(0);
		progressBar.setIndeterminate(false);
		fileTransferInProgress = true;
	}

	public void byteProgressUpdate(int len) {
		if (fileTransferInProgress) {
			byteProgress += (len/10); // WARNING This solution gives a correct progressBar for data up to 20 GB
			progressBar.setValue((int)byteProgress);
		}
	}

	public void fileInProgress() {
		fileProgress += 1;
		progressBar.setString(fileProgress + " / " + filesExpected);
	}

	public void filesTransferFinished() {
		fileTransferInProgress = false;
		progressBar.setVisible(false);
	}
}
